package com.example.testsqlite;

import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OperationInsert extends Activity {
	TextView textTableName;
	EditText editAttribute1;
	EditText editAttribute2;
	EditText editAttribute3;
	TextView textAttribute1;
	TextView textAttribute2;
	TextView textAttribute3;
	String Attribute1;
	String Attribute2;
	String Attribute3;
	Button btnInsertOk;
	public static int i = 0;

	String table;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_operation_insert);
		
		textTableName = (TextView) findViewById(R.id.text_table_name);
		editAttribute1 = (EditText) findViewById(R.id.edit_attribute1);
		editAttribute2 = (EditText) findViewById(R.id.edit_attribute2);
		editAttribute3 = (EditText) findViewById(R.id.edit_attribute3);
		textAttribute1 = (TextView) findViewById(R.id.text_attribute1);
		textAttribute2 = (TextView) findViewById(R.id.text_attribute2);
		textAttribute3 = (TextView) findViewById(R.id.text_attribute3);
		btnInsertOk = (Button) findViewById(R.id.btn_insert_ok);

		Intent intent = getIntent();
		table = intent.getStringExtra("table");

		List<String> tables = CreateTable.tableAndAttributeList;
		int index = tables.indexOf(table);
		textTableName.setText(table);
		textAttribute1.setText(tables.get(index + 1));
		textAttribute2.setText(tables.get(index + 2));
		textAttribute3.setText(tables.get(index + 3));
		
		Attribute1 = editAttribute1.getText().toString();
		Attribute2 = editAttribute2.getText().toString();
		Attribute3 = editAttribute3.getText().toString();

		btnInsertOk.setOnClickListener(btnInsertOkListener);

	}

	private OnClickListener btnInsertOkListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			ContentValues contentValues = new ContentValues();
			contentValues.put("id",i++);
			contentValues.put(textAttribute1.toString(), Attribute1);
			contentValues.put(textAttribute2.toString(), Attribute2);
			contentValues.put(textAttribute3.toString(), Attribute3);
			long in = MainActivity.sqliteDatabase.insert(table, null, contentValues);
			Log.i("gjf", "+++++++++++++++++++++++++++++++" + in);
			setResult(Operation.RESULT_OK);
			finish();		
		}
	};

}
